﻿using System;
using System.Collections.Generic;
using SkypeBot.Core;
using SkypeBot.Core.Properties;

namespace SkypeBot.Commands
{
    public static class GameCache
    {
        public static int index = 0;
        public static List<string> Questions = new List<string>();
        public static Dictionary<string, string> QuestionCache = new Dictionary<string, string>();

        public static string GetCurrentQuestion()
        {
            return Questions[index];
        }

        public static void Shuffle<T>(this IList<T> list)
        {
            Random rng = new Random();
            int n = list.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                T value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }

        public static void Reset()
        {
            Questions = new List<string>();
            QuestionCache = new Dictionary<string,string>();
        }
    }

    public class GameCommand : IMessageHandler
    {
        [Message(@"^!gamestart$|играть$|грати$")]
        public Responce StartGame(Message message)
        {
            try
            {
                
                GameCache.Reset();
                string content = Resources.QuestionBase; //string.Empty;
                //using (StreamReader r = new StreamReader(pathToFile, UTF8Encoding.UTF8))
                //{

                //    content = r.ReadToEnd();
                //}
                var list = content.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                foreach (var item in list)
                {
                    var pair = item.Split('|');
                    if (!GameCache.QuestionCache.ContainsKey(pair[0]) && pair.Length == 2)
                    {
                        GameCache.QuestionCache.Add(pair[0], pair[1]);
                        GameCache.Questions.Add(pair[0]);

                    }
                }
                GameCache.Shuffle<String>(GameCache.Questions);

                return new Responce(GameCache.GetCurrentQuestion());
            }
            catch (Exception ex)
            {
                return new Responce(ex.Message);
            }
        }

        [Message(@"^!next$|дальше$|далі$")]
        public Responce Next(Message objMessage)
        {
            if (GameCache.QuestionCache.Count == 0)
                return new Responce("Игра не начата");
           
            GameCache.index += 1;

            var res = string.Format("{0} - слаб и предал бота,{1}Следующий вопрос - {2} ", 
                                        objMessage.SenderName, 
                                        Environment.NewLine, 
                                        GameCache.Questions[GameCache.index]);
            
            return new Responce(res);

        }

        [Message(@"^!answer|ответ|відповідь (?<answer>.*)")]
        public Responce Answer(Message objMessage, string answer)
        {
            if (GameCache.QuestionCache.Count == 0)
                return new Responce("Игра не начата");

            string res = string.Empty;

            if (GameCache.QuestionCache[GameCache.GetCurrentQuestion()] == answer)
            {
                GameCache.index += 1;
                res = string.Format("{0} - красава,{1}Следующий вопрос - {2}", 
                                        objMessage.SenderName, 
                                        Environment.NewLine, 
                                        GameCache.Questions[GameCache.index]);

            }
            else
            {
                res = string.Format("{0} - слабоват,{1}Повторю вопрос - {2} ", 
                                        objMessage.SenderName, 
                                        Environment.NewLine, 
                                        GameCache.Questions[GameCache.index]);
            }

            return new Responce(res);
        }
    }
}
